const OrdercqRepositories = require('../repositories/ordercqRepositories')

class OrdercqServices {

  handleResponse(data, error, notFoundMessage = '实体未找到') {
    if (error) {
      return {
        code: 1,
        msg: error.message,
        statusCode: error.statusCode || 500
      }
    }
    if (!data) {
    return {
      code: 1,
      msg: notFoundMessage,
      statusCode: 404,
    }
  }
    return { code: 0, data: data }
  }

  async addNewOrdercq(ordercqData) {
    const newOrdercq = await OrdercqRepositories.addNewOrdercq(ordercqData)
    return newOrdercq ? this.handleResponse(newOrdercq, null, null) : this.handleResponse(null, null, '添加失败')
  }


  async delOrdercqById(id) {
    const bool = await OrdercqRepositories.delOrdercqById(id)
    return bool ? this.handleResponse('删除成功', null, null) : this.handleResponse(null, '删除失败', null)
  }

  async getOrdercqByCunId(id) {
    const ordercq = await OrdercqRepositories.getOrdercqByCunId(id)
    return this.handleResponse(ordercq, null, null)
  }

  async getAllOrdercqs() {
    try {
      const ordercqs = await OrdercqRepositories.getAllOrdercqs();
      return this.handleResponse(ordercqs, null, null);
    } catch (error) {
      return this.handleResponse(null, error, null)
    }
  }
}

module.exports = new OrdercqServices